package reader.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import model.ConfigModel;
import reader.ReaderCore;

public class XMLConfigReader implements ReaderCore {

	@SuppressWarnings("unchecked")
	@Override
	public Object readFile(String fileName, int subListType) throws DocumentException {
		Map<String, String> JBOconvertType = new HashMap<>();
		Map<String, String> convertType = new HashMap<>();
		String sourceFileEncodingType = null;
		
		// 加载配置
		SAXReader saxReader = new SAXReader();
		Document config = saxReader.read(new File(fileName));
		Element root = config.getRootElement();
		
		List<Element> nodes = root.element("JBOFields").elements("Field");
		for (Iterator<Element> iterator = nodes.iterator(); iterator.hasNext();) {
			Element element = (Element) iterator.next();
			JBOconvertType.put(element.attributeValue("Type").toUpperCase(), element.attributeValue("Convert"));
		}
		
		nodes = root.element("Fields").elements("Field");
		for (Iterator<Element> iterator = nodes.iterator(); iterator.hasNext();) {
			Element element = (Element) iterator.next();
			convertType.put(element.attributeValue("Type").toUpperCase(), element.attributeValue("Convert"));
		}
		
		sourceFileEncodingType = root.element("SourceFileEncodingType").attributeValue("Type");
		
		ConfigModel.setConfig("JBOconvertType", JBOconvertType);
		ConfigModel.setConfig("convertType", convertType);
		ConfigModel.setConfig("sourceFileEncodingType", sourceFileEncodingType);
		return null;
	}
}
